﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessLogic;
using QUANLIHOCSINH.BussinessObject;
namespace QUANLIHOCSINH
{
    //Khai Bao Cac Trang Thai Cua Form Quan Li Hoc Sinh
    enum Status_QuanLiHocSinh { View, Insert, Update, Delete };
    public partial class FrmQuanLiHocSinh : Form
    {
        Status_QuanLiHocSinh TrangThai = Status_QuanLiHocSinh.View;
        HOCSINHBUS hocsinh = new HOCSINHBUS();
        HOCSINH hs = new HOCSINH();
        GIOITINHBUS gioitinh = new GIOITINHBUS();
        public FrmQuanLiHocSinh()
        {
            InitializeComponent();
        }
        // Ham Hien Thi Button
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
            btnXoa.Visible = !Enable;
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;
            gbhocsinh.Enabled = Enable;
        }
        // Lay Thong Tin Hoc Sinh
        private HOCSINH LayThongTinHocSinh()
        {
            hs.mahocsinh = txtMaHocSinh.Text;
            hs.tenhocsinh = txtTenHocSinh.Text;
            hs.ngaysinh = txtNgaySinh.Text;
            hs.gioitinh = cbGioiTinh.SelectedValue.ToString();
            hs.email = txtEmail.Text;
            hs.diachi = txtDiaChi.Text;
            return hs;
        }
        private void ResetText()
        {
            txtMaHocSinh.Text = "";
            txtTenHocSinh.Text = "";
            txtNgaySinh.Text = "";

            txtEmail.Text = "";
            txtDiaChi.Text = "";
           
            
        }
        private void lbTenHocSinh_Click(object sender, EventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                HOCSINH hs = new HOCSINH();
                int r = dgvHocSinh.CurrentCell.RowIndex;
                hs.mahocsinh = dgvHocSinh.Rows[r].Cells[0].Value.ToString();
                hocsinh.XoaHocSinh(hs);
               
                EnableButton(false);
                FrmQuanLiHocSinh_Load(sender, e);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvHocSinh.Rows.Count > 1)
            {
                int r = dgvHocSinh.CurrentRow.Index;
                txtMaHocSinh.Text = dgvHocSinh.Rows[r].Cells["MaHocSinh"].Value.ToString();
                txtTenHocSinh.Text = dgvHocSinh.Rows[r].Cells["TenHocSinh"].Value.ToString();
                txtNgaySinh.Text = dgvHocSinh.Rows[r].Cells["NgaySinh"].Value.ToString();
                txtEmail.Text = dgvHocSinh.Rows[r].Cells["Email"].Value.ToString();
                txtDiaChi.Text = dgvHocSinh.Rows[r].Cells["DiaChi"].Value.ToString();
                cbGioiTinh.SelectedValue = dgvHocSinh.Rows[r].Cells["ColGioiTinh"].Value.ToString();
            }
          // dgvHocSinh

            EnableButton(true);
            btnSua.Visible = false;
             TrangThai = Status_QuanLiHocSinh.Update;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ResetText();
            txtMaHocSinh.Text = hocsinh.NextID();
            EnableButton(true);
            
            btnThem.Visible = false;
            TrangThai = Status_QuanLiHocSinh.Insert;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            EnableButton(false);
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            HOCSINH hs = LayThongTinHocSinh();
            if (TrangThai == Status_QuanLiHocSinh.Insert)
            {
                if (hocsinh.ThemHocSinh(hs) == true)
                {
                    ResetText();
                    EnableButton(false);
                    FrmQuanLiHocSinh_Load(sender,e);
                }
                else
                {
                    EnableButton(true);
                }
            }
            if (TrangThai == Status_QuanLiHocSinh.Update)
            {
                if (hocsinh.SuaHocSinh(hs) == true)
                {
                   
                    EnableButton(false);
                    FrmQuanLiHocSinh_Load(sender, e);
                }
                else
                {
                    EnableButton(true);
                }
            }
        }

        private void FrmQuanLiHocSinh_Load(object sender, EventArgs e)
        {
            if (hocsinh.DemSoLuong() > 0)
            {
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            (dgvHocSinh.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).DataSource = gioitinh.LayDanhSachGioiTinh();
            (dgvHocSinh.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).DisplayMember = "TenGioiTinh";
            (dgvHocSinh.Columns["ColGioiTinh"] as DataGridViewComboBoxColumn).ValueMember = "GioiTinh";

            cbGioiTinh.DataSource = gioitinh.LayDanhSachGioiTinh();
            cbGioiTinh.DisplayMember = "TenGioiTinh";
            cbGioiTinh.ValueMember = "GioiTinh";

            dgvHocSinh.DataSource = hocsinh.LayDanhSachHocSinh();
            EnableButton(false);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
